package com.apes.framework.plugin.websocket;

import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.NetUtil;
import com.apes.framework.util.PropertiesUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 功能：ws Service
 *
 * @author xul
 * @create 2018-08-21 15:14
 */
@Service
public class WsService {
    private String wsUrl = null;

    public String getWsUrl(SimpleRequest request) {
        PropertiesUtils propertiesUtils = SpringManager.getBean(PropertiesUtils.class);
        String wsUrl = propertiesUtils.getProperty("ws.url");
        if (StringUtils.isEmpty(wsUrl)) {
            if (StringUtils.isEmpty(this.wsUrl)) {
                this.wsUrl = String.format("ws://%s:%s/websocket", NetUtil.getServerIp(), propertiesUtils.getProperty("server.port"));
            }
            wsUrl = this.wsUrl;
        }
        return wsUrl;
    }

    public String getReportUrl(SimpleRequest request) {
        PropertiesUtils propertiesUtils = SpringManager.getBean(PropertiesUtils.class);
        String reportUrl = propertiesUtils.getProperty("report.url");
        if (StringUtils.isEmpty(reportUrl)) {
            reportUrl = String.format("http://%s:%s/ureport/pdf/show?_u=oracle:", NetUtil.getServerIp(), propertiesUtils.getProperty("server.port"));
        }
        return reportUrl;
    }

    public String getUReportUrl(SimpleRequest request) {
        PropertiesUtils propertiesUtils = SpringManager.getBean(PropertiesUtils.class);
        String reportUrl = propertiesUtils.getProperty("report.url");
        return reportUrl.substring(0,reportUrl.indexOf("pdf"));
    }
}
